package org.jeecg.modules.utils.beans;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.jeecg.modules.utils.dto.SmsDto;
import org.jeecg.modules.utils.service.IAlertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SendDingdingBeanInvoker {
    @Autowired
    private IAlertService alertService;

    @SneakyThrows
    @Async
    public void invoke(BaseFlowDto baseFlowDto) {
        Object data = baseFlowDto.getData();
        JSONObject extraParam = baseFlowDto.getExtraParam();
        String templateCode = extraParam.getString("code");
        SmsDto smsDto = new SmsDto();

        Map<String, Object> dataMap = BeanUtil.beanToMap(data);
        smsDto.setData(dataMap);
        smsDto.setTemplateCode(templateCode);
        smsDto.setOnce(true);

        alertService.send(smsDto);
    }
}
